package cz.muni.fi.PA165.dao;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import cz.muni.fi.PA165.entities.Author;
import cz.muni.fi.PA165.entities.IllegalEntityException;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

/**
 *
 * @author xmacejov
 */
@Stateless
public class AuthorDAOImpl implements AuthorDAO {

    @PersistenceContext(unitName = "author-unit", type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    @Override
    public void createAuthor(Author author) {
        if (author == null) {
            throw new IllegalArgumentException("author is null");
        }
        if (author.getId() != null) {
            throw new IllegalEntityException();
        }
        if (author.getName() == null) {
            throw new IllegalArgumentException("author wihtout name");
        }
        entityManager.persist(author);

    }

    @Override
    public Author getAuthor(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        Author author = entityManager.find(Author.class, id);
        return author;
    }

    @Override
    public List<Author> getAllAuthors() {
        Query query = entityManager.createQuery(
                "SELECT author FROM Author author");
        List<Author> authors = query.getResultList();
        return authors;
    }

    @Override
    public void updateAuthor(Author author) {

        if (author == null || author.getId() == null || author.getName() == null) {
            throw new IllegalArgumentException("author is null");
        }

        Author authorToUpdate = entityManager.find(Author.class, author.getId());

        if (authorToUpdate == null) {
            throw new IllegalEntityException();
        }

        if (!author.getName().equals(authorToUpdate.getName())) {
            authorToUpdate.setName(author.getName());
        }

        entityManager.persist(authorToUpdate);

    }

    @Override
    public void deleteAuthor(Author author) {
        if (author == null) {
            throw new IllegalArgumentException("author is null");
        }
        if (author.getId() == null) {
            throw new IllegalEntityException("author id is null");
        }
        Author authorToDelete = entityManager.find(Author.class, author.getId());

        if (authorToDelete == null) {
            throw new IllegalArgumentException();
        }

        if (!(author.getId().equals(authorToDelete.getId()))
                || !(authorToDelete.getName().equals(author.getName()))) {
            throw new IllegalArgumentException("trying to delete wrong author");
        }


        entityManager.remove(authorToDelete);


    }
}
